package cn.jarlen.android.plugin


import java.text.SimpleDateFormat
import java.util.regex.Matcher
import java.util.regex.Pattern

class VerUtils {

    static boolean checkGitProject() {
        def retText = ""
        try {
            def cmd = 'git rev-parse --is-inside-work-tree'
            def ret = cmd.execute().text
            retText = ret != null ? ret.trim().toLowerCase() : ""
        } catch (ignored) {
            println("> ApkPgVerPlugin ---> execute git command exception,please check")
        }
        return "true" == retText
    }

    static def getGitUserName() {
        def cmd = "git config user.name"
        def ret = cmd.execute().text
        ret = ret != null ? ret.trim() : ""
        return ret
    }

    static def getGitRepUrl() {
        def cmd = "git remote get-url --push origin"
        def ret = cmd.execute().text
        ret = ret != null ? ret.trim() : ""
        return ret
    }

    static def getGitBranch() {
        //判断是否处于Jenkins编译环境
        boolean flag = isInJenkins()
        if (flag) {
            def env = System.getenv()
            def branch = env.GIT_BRANCH
            String[] stringArray = branch.split("/")
            if (stringArray.length > 0) {
                return stringArray[stringArray.length - 1]
            } else {
                return "UnKnown Branch"
            }
        } else {
            def cmd = 'git symbolic-ref --short -q HEAD'
            def ret = cmd.execute().text;
            return ret != null ? ret.trim() : ""
        }
    }

/**
 * 获取Git 版本号
 */
    static def getGitSHA() {
        def cmd = 'git rev-parse HEAD'
        def ret = cmd.execute().text;
        return ret != null ? ret.trim() : ""
    }

    /**
     * 判断是否有jenkins
     */
    static boolean isInJenkins() {
        Map<String, String> map = System.getenv()
        if (map == null) {
            return false
        }
        String str = map.get("Path")
        if (str != null) {
            //it's windows
            return false
        } else {
            str = ""
            Iterator it = map.iterator()
            while (it.hasNext()) {
                str += it.next()
            }
            return str.contains("jenkins")
        }
    }

    static def getDate() {
        SimpleDateFormat sdf = new SimpleDateFormat('MMM d yyyy', Locale.CHINA);
        return sdf.format(new Date());
    }

    static boolean checkSVNProject(File projectRootFile) {
        if (projectRootFile == null || !projectRootFile.exists()) {
            return false
        }
        def svnConfFile = new File(projectRootFile, ".svn")
        if (svnConfFile.exists()) {
            return true
        }
        return checkSVNProject(projectRootFile.getParentFile())
    }

    static def getSvnRepUrl() {
        def svnRepUrl = ""
        try {
            def cmd = "svn info --show-item url"
            def ret = cmd.execute();
            ret.waitFor();
            svnRepUrl = ret.in.text
        } catch (ignored) {
            println("> ApkPgVerPlugin ---> execute svn command exception,please check")
        }
        return svnRepUrl
    }

    static def getSvnUserName() {
        def userNameInfo = ""
        try {
            def cmd = "svn auth svn.simple"
            def ret = cmd.execute()
            ret.waitFor()
            userNameInfo = ret.in.text
        } catch (ignored) {
            println("> ApkPgVerPlugin ---> execute svn command exception,please check")
        }
        userNameInfo = userNameInfo != null ? userNameInfo.trim().toLowerCase() : ""
        Matcher m = Pattern.compile("username:.*?\r\n").matcher(userNameInfo);
        userNameInfo = m.find() ? m.group(m.groupCount()).replace("username:", "").replace("\r\n", "") : ""
        return userNameInfo.trim()
    }

    static def getSvnVersion() {
        def versionText = ""
        try {
            def cmd = "svnversion -c "
            def ret = cmd.execute()
            ret.waitFor()
            versionText = ret.in.text;
        } catch (ignored) {
            println("> ApkPgVerPlugin ---> execute svn command exception,please check")
        }
        def reg = "(\\d+\\:)?(\\d+)\\D?"
        Matcher m = Pattern.compile(reg).matcher(versionText);
        if (m.find()) {
            versionText = m.group(m.groupCount());
        }

        return versionText
    }

}